/**
 * @title AbsBindPhoneActivity.java
 * @package com.esun.cheetah.view.personal.bindphone
 * @description 
 * @author liugan
 * @date 2012-4-25
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.personal.bindphone;

import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.network.AbsInferiorityCommunicater;
import com.esun.cheetah.view.custom.CustomAlertDialog;
import com.esun.cheetah.view.custom.CustomProgressDialog;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

/**
 * @title AbsBindPhoneActivity
 * @description
 * @author liugan
 */
public class AbsBindPhoneActivity extends Activity {

	protected EditText etOldNumber;
	protected EditText etPhoneNumber;
	protected EditText etCode;
	protected Handler mSendCodeHandler;
	protected Handler mBindHandler;
	private CustomAlertDialog mDialog;
	private CustomProgressDialog mProgressDialog;
	protected AbsInferiorityCommunicater mCommunicater;
	protected Button btnCancel;
	protected String mPhoneNumber;

	protected Button btnBind;
	protected Button btnSendCode;

	private AsyncTask<Integer, Integer, Void> mCountDownTask;

	protected void showDialog(CharSequence message) {
		if (mDialog == null) {
			mDialog = new CustomAlertDialog(AbsBindPhoneActivity.this);
		}
		mDialog.setMessage(message);
		mDialog.show();
	}

	protected void showProgressDialog(CharSequence message) {
		if (mProgressDialog == null) {
			mProgressDialog = new CustomProgressDialog(
					AbsBindPhoneActivity.this);
		}
		mProgressDialog.setMessage(message);
		mProgressDialog.show();
	}

	protected void dismissProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
	}

	@Override
	protected void onResume() {
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				cancelCountDown();
				finish();
			}
		});
		super.onResume();
	}

	protected void cancelCountDown() {
		if (mCountDownTask != null) {
			if (mCountDownTask.getStatus() != AsyncTask.Status.FINISHED) {
				mCountDownTask.cancel(true);
			}
		}
	}

	class SendCodeHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			btnSendCode.setEnabled(true);
			btnSendCode.setText(Language.SEND_CODE);
			switch (msg.what) {

			case Config.COMMUNICATER_SUCCESS:
				if (etOldNumber != null) {
					etOldNumber.clearFocus();
				}
				etPhoneNumber.clearFocus();
				etCode.requestFocus();
				showDialog(Html
						.fromHtml("<center>短信验证码已发送到<br/><font color=green >"
								+ msg.obj
								+ "</font><br/>请及时查阅。<br/>重新获取验证码，请等待一分钟！</center>"));
				mCountDownTask = new CountDownTask(btnSendCode);
				mCountDownTask.execute(60);
				break;
			case Config.COMMUNICATER_FAIL:
				showDialog(msg.obj.toString());
				break;
			case Config.COMMUNICATER_EXCEPTION:
			default:
				dismissProgressDialog();
				showDialog(Language.SEND_CODE_FAIL);
				break;
			}

		}

	}

	class BindHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Config.COMMUNICATER_SUCCESS:
				UserInfo.getUserInfo().setBindmobile(mPhoneNumber);
				finish();
				break;
			case Config.COMMUNICATER_FAIL:
				dismissProgressDialog();
				showDialog(msg.obj.toString());
				break;
			case Config.COMMUNICATER_EXCEPTION:
			default:
				dismissProgressDialog();
				showDialog(Language.BIND_MOBILE_FAIL);
				break;
			}
		}
	}

	class CountDownTask extends AsyncTask<Integer, Integer, Void> {
		private Button mButton;

		public CountDownTask(Button mButton) {
			this.mButton = mButton;
			mButton.setEnabled(false);
		}

		@Override
		protected Void doInBackground(Integer... params) {
			try {
				final int count = params[0];
				publishProgress(count);
				for (int i = count; i > 0; i--) {
					Thread.sleep(1000);
					if (!this.isCancelled()) {
						publishProgress(i);
					}
				}

			} catch (InterruptedException e) {
				mButton = null;
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			if (mButton != null) {
				mButton.setText(values[0] + Language.COUNT_DOWN_SECOND);
			}
		}

		@Override
		protected void onCancelled() {
			mButton = null;
		}

		@Override
		protected void onPostExecute(Void result) {
			mButton.setText(Language.SEND_CODE);
			mButton.setEnabled(true);
		}

	}

	protected void hideSoftKeys() {
		InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		im.hideSoftInputFromWindow(AbsBindPhoneActivity.this.getCurrentFocus()
				.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

	}

}
